<?php
/*
 * excel批量导入老人信息
 */
class Apiv1_ImportseniorController extends Ctrl_Api{
    public function indexAction(){
        Yaf_Loader::import('Source/PHPExcel/PHPExcel.php');
        Yaf_Loader::import('Source/PHPExcel/PHPExcel/IOFactory.php');
        Yaf_Loader::import('Source/PHPExcel/PHPExcel/Excel5.php');
        
        $objReader = PHPExcel_IOFactory::createReader('Excel2007');//use Excel5 for 2003 format
        $tExcel = $_FILES['file_stu'];//接收上传的excel文件
        $tEName = $tExcel['name'];//上传的excel名称
        $tPname = $tExcel['tmp_name'];
        if(!move_uploaded_file($tPname,APPLICATION_PATH.'/public/excel/'.$tEName)){
            alert('操作失败');exit;
        } 
        $excelpath = APPLICATION_PATH.'/public/excel/'.$tEName;
        $objPHPExcel = $objReader->load($excelpath);
        //设置单元格格式为文本
        $objPHPExcel->getActiveSheet()->getStyle('G')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
        $objPHPExcel->getActiveSheet()->getStyle('J')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();//取得总行数
        $highestColumn = $sheet->getHighestColumn();//取得总列数
        $tArr = array(array());
        for($j=2;$j<=$highestRow;$j++){//从第二行开始读取数据
            $str = '';
            for($k=ord('A');$k<=ord($highestColumn);$k++){//从A列读取数据
                //设置单元格格式为数值格式    
                $str .= $objPHPExcel->getActiveSheet()->getCell(chr($k).$j)->getValue().'|*|';//读取单元格;
                $str = mb_convert_encoding($str,'UTF8','auto');//根据自己编码修改
            }
            $strs = explode('|*|',$str);            
            $tArr[$j] = $strs;
        }
        unset($tArr[0]);
        var_dump($tArr);exit;
        if(!empty($tArr)){
            $tSMO = new SeniorModel;
            $tMIMO = new MedicalinsuranceModel;
            $tDIMO = new DepartmentinfoModel;
            $tDiMO = new DiscountinfoModel;
            $tPIMO = new PaymentinfoModel;
            $tFMMO = new FamilymembersModel;

            $tTime = time();

            foreach($tArr as $tK=>$tV){
                if($tArr[$tK][0] == 1){//判断是主表还是附表
                    $tSData = $tSMO->field('id')->where('critcode = \''.trim($tArr[$tK][2]).'\'')->fRow();
                    if(empty($tSData['id'])){Tool_Fnc::ajaxMsg('证件号码'.trim($tArr[$tK][2],'`').'的老人不存在!');}
                    //委托人信息
                    $tData = array(
                        'sid' => $tSData['id'],
                        'name' => $tArr[$tK][3],
                        'relation' => $tArr[$tK][4],
                        'is_client' => 1,
                        'phone' => $tArr[$tK][5],
                        'is_talk' => $this->isTalk($tArr[$tK][6]),
                        'address' => $tArr[$tK][7],
                        'critcode' => trim($tArr[$tK][8],'`'),
                        'created' => $tTime,
                    );
                    if(!$tFMMO->insert($tData)){
                        Tool_Fnc::ajaxMsg('证件号码'.trim($tArr[$tK][2],'`').'的老人委托人添加失败 004');
                    }
                    //老人附属信息
                    $tData = array(
                        'diet' => $tArr[$tK][9],
                        'life' => $tArr[$tK][10],
                        'sleep' => $tArr[$tK][11],
                        'medical_orders' => $tArr[$tK][12],
                        'physical_examination' => $tArr[$tK][13],
                        'discharge_diagnosis' => $tArr[$tK][14],
                        'goods' => $tArr[$tK][15],
                        'updated' => $tTime,
                        'id' => $tSData['id'],
                    );
                    if(!$tSMO->update($tData)){
                        Tool_Fnc::ajaxMsg('证件号码'.trim($tArr[$tK][2],'`').'的老人附属信息添加失败 005');
                    }
                }
                $tSData = $tSMO->field('id')->where('critcode = \''.trim($tArr[$tK][6],'`').'\'')->fRow();
                if(!empty($tSData['id'])){
                    Tool_Fnc::ajaxMsg('证件号码'.trim($tArr[$tK][6],'`').'已存在!');
                }else{
                    //所在部门查询
                    $tDepartment = $tDIMO->field('num')->where('introduce = \''.$tArr[$tK][13].'\'')->fRow();
                    //医保收费折扣查询
                    $tDiscount = $tDiMO->field('num')->where('introduce = \''.$tArr[$tK][11].'\'')->fRow();
                    //支付方式查询
                    $tPayment = $tPIMO->field('num')->where('introduce = \''.$tArr[$tK][12].'\'')->fRow();

                    $tData = array(
                        'name' => $tArr[$tK][0],
                        'sex' => $this->sexStatus($tArr[$tK][1]),
                        'age' => $tArr[$tK][2],
                        'nation' => $this->nationStatus($tArr[$tK][3]),
                        'religion' => $this->religionStatus($tArr[$tK][4]),
                        'permanent_residence' => $tArr[$tK][5],
                        'critcode' => trim($tArr[$tK][6],'`'),
                        'birth_date' => $tArr[$tK][7],
                        'married_status' => $this->marriedStatus($tArr[$tK][8]),
                        'department' => $tDepartment['num'],
                        'education_levels' => $this->educationStatus($tArr[$tK][14]),
                        'body_status' => $this->bodyStatus($tArr[$tK][15]),
                        'medical_status' => $this->medicalStatus($tArr[$tK][16]),
                        'old_address' => $tArr[$tK][17],
                        'old_company' => $tArr[$tK][18],
                        'temper_hpbby' => $tArr[$tK][19],
                        'allergies' => $tArr[$tK][20],
                        'created' => $tTime,
                    );
                    if(!$tId = $tSMO->insert($tData)){
                        Tool_Fnc::ajaxMsg('证件号码'.trim($tArr[$tK][13]).'的老人添加失败 001');
                    }
                    //医保信息
                    $tData = array(
                        'sid' => $tId,
                        'card_number' => $tArr[$tK][9], 
                        'basic_fee' => $tArr[$tK][10],
                        'charge_discount' => $tDiscount['num'],
                        'payment' => $tPayment['num'],
                        'created' => $tTime,
                    );
                    if(!$tMIMO->insert($tData)){
                        Tool_Fnc::ajaxMsg('医保信息添加出错 002');
                    }
                }
            }
            Tool_Fnc::ajaxMsg('添加成功',1);
        }
    }
    //性别转换
    private function sexStatus($tSex){
        switch($tSex){
            case '男':return 1;break;
            case '女':return 2;break;
            default:return 0;break;    
        }
    }
    //欠费状态
    private function isTalk($tIstalk){
        switch($tIstalk){
            case '否':return 1;break;
            case '是':return 2;break;
        }
    }
    //民族转换
    private function nationStatus($tNation){
        switch($tNation){
            case '汉族':return 1;break;
            case '蒙古族':return 2;break;
            case '回族':return 3;break;
            case '藏族':return 4;break;
            case '维吾尔族':return 5;break;
            case '苗族':return 6;break;
            case '彝族':return 7;break;
            case '壮族':return 8;break;
            case '布依族':return 9;break;
            case '朝鲜族':return 10;break;
            case '满族':return 11;break;
            case '侗族':return 12;break;
            case '瑶族':return 13;break;
            case '白族':return 14;break;
            case '土家族':return 15;break;
            case '哈尼族':return 16;break;
            case '哈萨克族':return 17;break;
            case '傣族':return 18;break;
            case '黎族':return 19;break;
            case '傈傈族':return 20;break;
            case '佤族':return 21;break;
            case '畲族':return 22;break;
            case '高山族':return 23;break;
            case '拉祜族':return 24;break;
            case '水族':return 25;break;
            case '东乡族':return 26;break;
            case '纳西族':return 27;break;
            case '景颇族':return 28;break;
            case '柯尔克孜族':return 29;break;
            case '土族':return 30;break;
            case '达翰尔族':return 31;break;
            case '仫佬族':return 32;break;
            case '羌族':return 33;break;
            case '布朗族':return 34;break;
            case '撒拉族':return 35;break;
            case '毛南族':return 36;break;
            case '仡佬族':return 37;break;
            case '锡伯族':return 38;break;
            case '阿昌族':return 39;break;
            case '普米族':return 40;break;
            case '塔吉克族':return 41;break;
            case '怒族':return 42;break;
            case '乌孜别克族':return 43;break;
            case '俄罗斯族':return 44;break;
            case '鄂温克族':return 45;break;
            case '德昂族':return 46;break;
            case '保安族':return 47;break;
            case '裕固族':return 48;break;
            case '京族':return 49;break;
            case '塔塔尔族':return 50;break;
            case '独龙族':return 51;break;
            case '鄂伦春族':return 52;break;
            case '赫哲族':return 53;break;
            case '门巴族':return 54;break;
            case '珞巴族':return 55;break;
            case '基诺族':return 56;break;
        }
    }
    //宗教转换
    private function religionStatus($tReligion){
        switch($tReligion){
            case '佛教':return 1;break;
            case '道教':return 2;break;
            case '伊斯兰教':return 3;break;
            case '基督教':return 4;break;
            case '天主教':return 5;break;
            default:return 0;break;
        }
    }
    //婚姻转换
    private function marriedStatus($tMarried){
        switch($tMarried){
            case '未婚':return 1;break;
            case '已婚':return 2;break;
            case '离异':return 3;break;
            case '丧偶':return 4;break;
        }
    }
    //文化程度转换
    private function educationStatus($tEducationstatus){
        switch($tEducationstatus){
            case '文盲/半文盲':return 1;break;
            case '小学':return 2;break;
            case '初中':return 3;break;
            case '高中':return 4;break;
            case '技工学校':return 5;break;
            case '中专/中技':return 6;break;
            case '大专':return 7;break;
            case '本科':return 8;break;
            case '硕士':return 9;break;
            case '博士':return 10;break;
        }
    }
    //医保状况转换
    private function medicalStatus($tMedical){
        switch($tMedical){
            case '有':return 1;break;
            case '无':return 2;break;
        }
    }
    //身体状况转换
    private function bodyStatus($tBodystatus){
        switch($tBodystatus){
            case '半自理1级':return 1;break;
            case '半自理2级':return 2;break;
            case '半自理3级':return 3;break;
            case '完全不能自理1级':return 4;break;
            case '完全不能自理2级':return 5;break;
            case '完全不能自理3级':return 6;break;
            case '可自理':return 7;break;
        }
    }

}
?>
